package fr.teuteuf.runstickmanrun.map.block;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.physics.box2d.World;

import fr.teuteuf.runstickmanrun.Constants;
import fr.teuteuf.runstickmanrun.map.MapGenerator;
import fr.teuteuf.runstickmanrun.map.tile.DamagingBoxTile;
import fr.teuteuf.runstickmanrun.map.tile.SolidBoxTile;
import fr.teuteuf.runstickmanrun.map.tile.Tile;

public class ObstructedPathsBlock extends AbstractMapBlock {
	private final int PATH_HEIGHT = 3;
	private final int PATH_WIDTH  = Constants.WIDTH_RESOLUTION / Tile.TILE_SIZE;
	
	public ObstructedPathsBlock(int difficultyLevel) {
		setDifficulties(0, 1, 3);
		setCurrentLevel(difficultyLevel);
	}
	
	@Override
	public int generateBlock(World world, MapGenerator mg, int nbHorizontalTiles) {
		
		int blockLength = 0;
		int goodPath = MathUtils.random(2);
		
		mg.addTile(new SolidBoxTile((byte) 1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE, world));
		blockLength++;
		mg.addTile(new SolidBoxTile((byte) 1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 13, 6, 4, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 1 * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 13, 6, 4, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 3 * Tile.TILE_SIZE, world));
		blockLength++;
		mg.addTile(new SolidBoxTile((byte) 1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 5, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 1 * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 5, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 3 * Tile.TILE_SIZE, world));
		blockLength++;
		mg.addTile(new SolidBoxTile((byte) 1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 5, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 1 * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 13, 6, 4, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 2 * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 5, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 3 * Tile.TILE_SIZE, world));
		blockLength++;
		
		do {
			mg.addTile(new SolidBoxTile((byte) 1, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, 0 * Tile.TILE_SIZE, world));
			mg.addTile(new SolidBoxTile((byte) 5, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 1 * Tile.TILE_SIZE, world));
			mg.addTile(new SolidBoxTile((byte) 5, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 2 * Tile.TILE_SIZE, world));
			mg.addTile(new SolidBoxTile((byte) 5, 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 3 * Tile.TILE_SIZE, world));
			
		} while(blockLength++ < PATH_WIDTH);
		
		for(int i = 0; i < 3; i++) {
			if(i != goodPath && getCurrentLevel() >= getMedDifficultyLevel() || i == goodPath && getCurrentLevel() < getMedDifficultyLevel()) {
				for(int j = 1; j <= PATH_HEIGHT; j++) {
					mg.addTile(new DamagingBoxTile(2, 8, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (i * (PATH_HEIGHT + 1) + j) * Tile.TILE_SIZE, world, 10));
				
					if(getCurrentLevel() >= getMaxDifficultyLevel()) {
						mg.addTile(new DamagingBoxTile(2, 8, (nbHorizontalTiles + blockLength - 2) * Tile.TILE_SIZE, (i * (PATH_HEIGHT + 1) + j) * Tile.TILE_SIZE, world, 10));
					}
				}
			}
		}
		
		mg.addTile(new SolidBoxTile((byte) 15, 2, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 3 * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 15, 2, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 2 * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 15, 2, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 1 * Tile.TILE_SIZE, world));
		mg.addTile(new SolidBoxTile((byte) 1 , 1, 1, (nbHorizontalTiles + blockLength) * Tile.TILE_SIZE, (PATH_HEIGHT + 1) * 0 * Tile.TILE_SIZE, world));
		blockLength++;
		
		return nbHorizontalTiles + blockLength;
	}

}
